package com.cc.blog.common.utils;

import com.cc.blog.common.enums.CodeEnum;
import lombok.Data;

import java.io.Serializable;

/**
 * 响应类
 * @Author cc
 * @Date 2023/10/18 9:01
 */
@Data
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 699189837924801644L;
    /** 是否成功，默认为 true */
    private boolean success = true;
    /** 状态码，成功 0 */
    private String code;
    /** 提示语 */
    private String msg;
    /** 返回数据 */
    private T data;

    public Result() {}
    public Result(boolean success, String code, String msg,T data) {
        this.success = success;
        this.data = data;
        this.code = code;
        this.msg = msg;
    }

    // =================================== 成功响应 ===================================

    public static <T> Result<T> success(String code, String msg,T data) {
        return new Result<>(true, code, msg, data);
    }
    public static <T> Result<T> success() {
        return success(CodeEnum.SUCCESS.getCode(),CodeEnum.SUCCESS.getMsg(),null);
    }
    public static <T> Result<T> success(T data) {
        return success(CodeEnum.SUCCESS.getCode(),CodeEnum.SUCCESS.getMsg(),data);
    }
    public static <T> Result<T> success(T data,String msg) {
        return success(CodeEnum.SUCCESS.getCode(),msg,data);
    }

    // =================================== 失败响应 ===================================
    public static <T> Result<T> fail(String code, String msg,T data) {
        return new Result<>(false, code, msg, data);
    }
    public static <T> Result<T> fail() {
        return fail(CodeEnum.FAIL.getCode(),CodeEnum.FAIL.getMsg(),null);
    }
    public static <T> Result<T> fail(String errorMessage) {
        return fail(CodeEnum.SUCCESS.getCode(),errorMessage,null);
    }
    public static <T> Result<T> fail(String errorCode, String errorMessage) {
        return fail(errorCode,errorMessage,null);
    }
    public static <T> Result<T> fail(CodeEnum codeEnum) {
        return fail(codeEnum.getCode(),codeEnum.getMsg(),null);
    }
    public static <T> Result<T> fail(CodeEnum codeEnum, T data) {
        return fail(codeEnum.getCode(),codeEnum.getMsg(),data);
    }

}
